package com.google.gwt.it2tool.client.widget;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.it2tool.client.widget.MassProductionPanel.IMassProductionChangedHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CustomFactoryOrderPanel extends HorizontalPanel {
	
	private ArrayList<FactoryOrderRequestHandler> handlers = new ArrayList<FactoryOrderRequestHandler>();
	
	
	//Header
	private Label customFactoryOrderLabel = new Label("Fabriken sortieren");
	//Header+content
	private VerticalPanel body = new VerticalPanel();

	private Button customFactoryOrderButton = new Button("Fabrikreihenfolge ändern");
	private Button customFactoryOrderSaveButton = new Button("Fabrikreihenfolge speichern");
	
	public CustomFactoryOrderPanel(){
		super();
		
		customFactoryOrderLabel.addStyleName("addFactoryLabel");

		body.add(customFactoryOrderLabel);
		body.add(customFactoryOrderButton);
		add(body);
		addStyleName("addFactoryPanel");
		
		customFactoryOrderButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				triggerOnChangeFactoryOrder();
				body.remove(customFactoryOrderButton);
				body.add(customFactoryOrderSaveButton);
			}
		});

		customFactoryOrderSaveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				triggerOnSaveFactoryOrder();
				body.remove(customFactoryOrderSaveButton);
				body.add(customFactoryOrderButton);
			}
		});
		
	}
	
	public void addFactoryOrderRequestHandler(FactoryOrderRequestHandler handler){
		if (handler!=null){
			handlers.add(handler);
		}
	}
	
	public void removeFactoryOrderRequestHandler(FactoryOrderRequestHandler handler){
		if (handler!=null && handlers.contains(handler)){
			handlers.remove(handler);
		}
	}
	
	private void triggerOnChangeFactoryOrder(){
		for(FactoryOrderRequestHandler handler: handlers){
			handler.onChangeFactoryOrder();
		}
	}
	
	private void triggerOnSaveFactoryOrder(){
		for(FactoryOrderRequestHandler handler: handlers){
			handler.onSaveFactoryOrder();
		}
	}
	
	public interface FactoryOrderRequestHandler{
		public void onChangeFactoryOrder();
		public void onSaveFactoryOrder();
	}
}
